package com.situ.store.dao;

import java.util.List;

import com.situ.store.entity.User;
import com.situ.store.util.DBCP;
import com.situ.store.util.MD5Util;

public class UserDao {

	// 登录验证
	public List<User> selectUserByName(String username) {
		String sql = "SELECT * FROM t_user WHERE name = ?";
		return DBCP.excuteDQL(sql, User.class, username);
	}

	// 添加用户
	public void add(User user) {
		String sql = "INSERT INTO t_user(name,pwd,sex,nick_name,createby) VALUES(?,?,?,?,?)";
		DBCP.excuteDML(sql, user.getName(), user.getPwd(), user.getSex(), user.getNickName(), user.getCreateby());
	}

	// 删除用户
	public void del(Integer id) {
		String sql = "DELETE FROM t_user WHERE id = ?";
		DBCP.excuteDML(sql, id);
	}

	// 重置密码
	public void resetPwd(Integer id) {
		String sql = "UPDATE t_user SET pwd = '" + MD5Util.md5("123456") + "' WHERE id = ?";
		DBCP.excuteDML(sql, id);
	}

	// 修改用户
	public void edit(User user) {
		String sql = "UPDATE t_user SET name = ?, pwd = ?,sex = ?,nick_name = ?, createby = ? WHERE id = ?";
		DBCP.excuteDML(sql, user.getName(), user.getPwd(), user.getSex(), user.getNickName(), user.getCreateby(),
				user.getId());
	}

	// 根据用户名查用户
	public List<User> selectByName(String name) {
		String sql = "SELECT * FROM t_user WHERE name = ?";
		return DBCP.excuteDQL(sql, User.class, name);
	}

	// 查询所有用户
	public List<User> selectAll(Integer pageSize, Integer pageNum, String keyword) {
		String sql = "SELECT * FROM t_user WHERE INSTR(name, '" + keyword + "') > 0 LIMIT ?, ? ";
		return DBCP.excuteDQL(sql, User.class, pageSize, pageNum);
	}

	// 根据ID查询用户
	public List<User> selectById(Integer id) {
		String sql = "SELECT * FROM t_user WHERE id = ?";
		return DBCP.excuteDQL(sql, User.class, id);
	}

	// 修改密码
	public void changePwd(String newPwd, String name) {
		String sql = "UPDATE t_user SET pwd = ? WHERE name = ?";
		DBCP.excuteDML(sql, newPwd, name);
	}

	// 查数量
	public Long count(String keyword) {
		String sql = "SELECT COUNT(1) FROM t_user WHERE INSTR(`name`, '" + keyword + "') > 0";
		return DBCP.executeCount(sql);
	}


}
